package day8;

import org.junit.Test;

import java.io.*;

class Person implements Serializable{

    private static final long serivlVersionUID = 1L;
    private int id;
    private String name;
    private String name2;

    // 瞬态
    private transient String sex;

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public Person() {
    }

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Person(int id, String name, String sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

public class ObjectIODemo {

    @Test
    public void test3(){
        System.out.println(Integer.toHexString(1001));
    }

    @Test
    public void test(){
        File file = new File("E:\\vue\\yq_JAVA\\src\\main\\resources\\person.txt");
        try(
            OutputStream os = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(os);
            ){
            Person p = new Person(1001, "王小二", "男");
            oos.writeObject(p);
            System.out.println("序列化对象完成...");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void test2(){
        try(InputStream is = new FileInputStream("E:\\vue\\yq_JAVA\\src\\main\\resources\\person.txt");
            ObjectInputStream ois = new ObjectInputStream(is)
        ){
            Person p = (Person)ois.readObject();
            System.out.println(p);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
